package com.bankcomm.ui.specialwidget;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bankcomm.R;
import com.bankcomm.common.ConstantRepository;
import com.bankcomm.ui.specialwidget.LPLottery.LotteryGroup.Lottery;
import com.bankcomm.util.DensityUtil;
import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.CssStyle;
import com.rytong.tools.utils.Utils;

/**
 * 彩票控件
 * 
 * @authora
 * 
 * @date
 */
public class LPLottery extends Component {

	static int preNum=9;
	
	@Override
	public void initRealView(Activity activity, String nodeValue) {
		// TODO Auto-generated method stub
		// childrenList_ = new ArrayList();
		realView_ = new LotteryGroup(activity);
	}

	@Override
	public void mouldH() throws Exception {
		// TODO Auto-generated method stub
		super.mouldH();
		((LotteryGroup) realView_).initResource();
		String tempStr = null;
		String ballInfo = getPropertyByName("ball");
		JSONArray ballArray = new JSONArray(ballInfo);
		for (int i = 0; i < ballArray.length(); i++) {
			JSONObject singleBallObject = new JSONObject(ballArray.getString(i));
			String type = singleBallObject.getString("type");
			String num = singleBallObject.getString("number");
			Lottery lottery = ((LotteryGroup) realView_).newLottery();
			lottery.ballNum_ = Integer.valueOf(num);
			lottery.ballType_ = type;
			lottery.width_ = width_;
			lottery.initLotteryResource();
			lottery.addChildView();
		}

		// for(Object obj : childrenList_) {
		// tempStr = (String) obj;
		// String[] numType = tempStr.split("/");
		// Lottery lottery = ((LotteryGroup)realView_).newLottery();
		// lottery.ballNum_ = Integer.valueOf(numType[0]);
		// lottery.ballType_ = numType[1];
		// lottery.width_ = width_;
		// lottery.initLotteryResource();
		// lottery.addChildView();
		// }

	}

	class LotteryGroup extends LinearLayout implements CompositedComponent {
		// 资源
		private TextPaint paint_;
		private static final int GRAY_BALL = 0;
		private final int TEXT_WIDTH = Utils.getFontSize(23);
		private final int TEXT_HEIGHT =  Utils.getFontSize(23);
		private static final int COLUMN =9; // 列

		private int ballWidth = Utils.getScaledValueX(25);
		private int ballHeight =Utils.getScaledValueY(25);
		private int leftMargin =Utils.getScaledValueX(5); // 左边距
		private int topMargin = Utils.getScaledValueY(5); // 上边距
		private int rightMargin = Utils.getScaledValueX(10);

		// 大乐透
		Bitmap[] dltBallImg_;
		private static final int DLT_GRAY_BALL = 0;
		private static final int DLT_RED_BALL = 1;
		private static final int DLT_BLUE_BALL = 2;
		int dltRedNum_ = 0;
		int dltBlueNum_ = 0;
		TextView dltRedText;
		TextView dltBlueText;

		// 双色球
		Bitmap[] ssqBallImg_;
		private static final int SSQ_GRAY_BALL = 0;
		private static final int SSQ_RED_BALL = 1;
		private static final int SSQ_BLUE_BALL = 2;
		int ssqRedNum_ = 0;
		int ssqBlueNum_ = 0;
		TextView ssqRedText;
		TextView ssqBlueText;

		// 福彩3D-直选
		Bitmap[] fc3dzxBallImg_;
		private static final int FC3DZX_GRAY_BALL = 0;
		private static final int FC3DZX_RED_BALL = 1;
		int fc3dzxBaiNum_ = 0;
		int fc3dzxShiNum_ = 0;
		int fc3dzxGeNum_ = 0;
		TextView fc3dzxBaiText;
		TextView fc3dzxShiText;
		TextView fc3dzxGeText;

		// 福彩3D-组三
		Bitmap[] fc3dz3BallImg_;
		private static final int FC3DZ3_GRAY_BALL = 0;
		private static final int FC3DZ3_RED_BALL = 1;
		int fc3dz3RedNum_ = 0;
		TextView fc3dz3RedText;

		// 福彩3D-组六
		Bitmap[] fc3dz6BallImg_;
		private static final int FC3DZ6_GRAY_BALL = 0;
		private static final int FC3DZ6_RED_BALL = 1;
		int fc3dz6RedNum_ = 0;
		TextView fc3dz6RedText;

		// 排列三-直选
		Bitmap[] pl3zxBallImg_;
		private static final int PL3ZX_GRAY_BALL = 0;
		private static final int PL3ZX_RED_BALL = 1;
		int pl3zxBaiNum_ = 0;
		int pl3zxShiNum_ = 0;
		int pl3zxGeNum_ = 0;
		TextView pl3zxBaiText;
		TextView pl3zxShiText;
		TextView pl3zxGeText;

		// 排列三-组三
		Bitmap[] pl3z3BallImg_;
		private static final int PL3Z3_GRAY_BALL = 0;
		private static final int PL3Z3_RED_BALL = 1;
		int pl3z3RedNum_ = 0;
		TextView pl3z3RedText;

		// 排列三-组六
		Bitmap[] pl3z6BallImg_;
		private static final int PL3Z6_GRAY_BALL = 0;
		private static final int PL3Z6_RED_BALL = 1;
		int pl3z6RedNum_ = 0;
		TextView pl3z6RedText;

		// 排列五
		Bitmap[] pl5BallImg_;
		private static final int PL5_GRAY_BALL = 0;
		private static final int PL5_RED_BALL = 1;
		int pl5WanNum_ = 0;
		int pl5QianNum_ = 0;
		int pl5BaiNum_ = 0;
		int pl5ShiNum_ = 0;
		int pl5GeNum_ = 0;
		TextView pl5WanText;
		TextView pl5QianText;
		TextView pl5BaiText;
		TextView pl5ShiText;
		TextView pl5GeText;

		// 七星彩
		Bitmap[] qxcBallImg_;
		private static final int QXC_GRAY_BALL = 0;
		private static final int QXC_RED_BALL = 1;
		int qxcYiNum_ = 0;
		int qxcErNum_ = 0;
		int qxcSanNum_ = 0;
		int qxcSiNum_ = 0;
		int qxcWuNum_ = 0;
		int qxcLiuNum_ = 0;
		int qxcQiNum_ = 0;
		TextView qxcYiText;
		TextView qxcErText;
		TextView qxcSanText;
		TextView qxcSiText;
		TextView qxcWuText;
		TextView qxcLiuText;
		TextView qxcQiText;

		// 七乐彩
		Bitmap[] qlcBallImg_;
		private static final int QLC_GRAY_BALL = 0;
		private static final int QLC_RED_BALL = 1;
		int qlcRedNum_ = 0;
		TextView qlcRedText;

		// 生肖乐
		Bitmap[] sxlBallImg_;
		private static final int SXL_GRAY_BALL = 0;
		private static final int SXL_RED_BALL = 1;
		int sxlRedNum_ = 0;
		TextView sxlRedText;

		// 报文属性
		String lotteryType_;
		int width_;
		int height_;

		// View
		Activity bv_;
		LotteryGroup lotteryGroup;
		TextView numTextView;

		boolean isMould_ = false;
		
		

		public LotteryGroup(Context context) {
			super(context);
			bv_ = (Activity) context;
			lotteryGroup = this;

			setOrientation(LinearLayout.VERTICAL);

			numTextView = new TextView(context);
			numTextView.setHintTextColor(0xFF000000);
			numTextView.setHint(getContext().getString(R.string.notselectnumber));

			lotteryType_ = "SSQ";
			Object objKind = getPropertyByName("kind");
			if (null != objKind) {
				lotteryType_ = (String) objKind;
			}

			if (cssStyle_ != null) {
				Object objWidth = cssStyle_.getProperty(CssStyle.WIDTH);
				if (objWidth != null) {
					width_ = Integer.parseInt(Component.fixedPX((String) objWidth));
				}
			}
		}

		@Override
		public Component composited() {
			// TODO Auto-generated method stub
			return LPLottery.this;
		}

		public void initResource() {
			paint_ = new TextPaint();
			paint_.setAntiAlias(true);

			Bitmap ballYellow = ConstantRepository.decodeBitmap(getResources(), R.drawable.ball_yellow);
			Bitmap ballGray = ConstantRepository.decodeBitmap(getResources(), R.drawable.ball_gray);
			Bitmap ballBlue = ConstantRepository.decodeBitmap(getResources(), R.drawable.ball_blue);
			Bitmap ballRed = ConstantRepository.decodeBitmap(getResources(), R.drawable.ball_red);

			// 缩得一下
			ballYellow = DensityUtil.zoomFunction(ballYellow, ballWidth, ballHeight);
			ballGray = DensityUtil.zoomFunction(ballGray, ballWidth, ballHeight);
			ballBlue = DensityUtil.zoomFunction(ballBlue, ballWidth, ballHeight);
			ballRed = DensityUtil.zoomFunction(ballRed, ballWidth, ballHeight);

			if ("DLT".equals(lotteryType_)) {
				dltBallImg_ = new Bitmap[3];
				dltBallImg_[DLT_GRAY_BALL] = DensityUtil.zoomFunction(ballGray, ballWidth, ballHeight);
				dltBallImg_[DLT_RED_BALL] = DensityUtil.zoomFunction(ballYellow, ballWidth, ballHeight);
				dltBallImg_[DLT_BLUE_BALL] = DensityUtil.zoomFunction(ballYellow, ballWidth, ballHeight);

				dltRedText = new TextView(bv_);
				dltBlueText = new TextView(bv_);
			} else if ("SSQ".equals(lotteryType_)) {
				ssqBallImg_ = new Bitmap[3];
				ssqBallImg_[SSQ_GRAY_BALL] = ballGray;
				ssqBallImg_[SSQ_RED_BALL] = ballRed;
				ssqBallImg_[SSQ_BLUE_BALL] = ballBlue;

				ssqRedText = new TextView(bv_);
				ssqBlueText = new TextView(bv_);
			} else if ("FC3DZX".equals(lotteryType_)) {
				fc3dzxBallImg_ = new Bitmap[2];
				fc3dzxBallImg_[FC3DZX_GRAY_BALL] = ballGray;
				fc3dzxBallImg_[FC3DZX_RED_BALL] = ballYellow;

				fc3dzxBaiText = new TextView(bv_);
				fc3dzxShiText = new TextView(bv_);
				fc3dzxGeText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZX));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("FC3DZ3".equals(lotteryType_)) {
				fc3dz3BallImg_ = new Bitmap[2];
				fc3dz3BallImg_[FC3DZ3_GRAY_BALL] = ballGray;
				fc3dz3BallImg_[FC3DZ3_RED_BALL] = ballYellow;

				fc3dz3RedText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZ3));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("FC3DZ6".equals(lotteryType_)) {
				fc3dz6BallImg_ = new Bitmap[2];
				fc3dz6BallImg_[FC3DZ6_GRAY_BALL] = ballGray;
				fc3dz6BallImg_[FC3DZ6_RED_BALL] = ballYellow;

				fc3dz6RedText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZ6));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("PL3ZX".equals(lotteryType_)) {
				pl3zxBallImg_ = new Bitmap[2];
				pl3zxBallImg_[PL3ZX_GRAY_BALL] = ballGray;
				pl3zxBallImg_[PL3ZX_RED_BALL] = ballYellow;

				pl3zxBaiText = new TextView(bv_);
				pl3zxShiText = new TextView(bv_);
				pl3zxGeText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZX));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("PL3Z3".equals(lotteryType_)) {
				pl3z3BallImg_ = new Bitmap[2];
				pl3z3BallImg_[PL3Z3_GRAY_BALL] = ballGray;
				pl3z3BallImg_[PL3Z3_RED_BALL] = ballYellow;

				pl3z3RedText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZ3));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("PL3Z6".equals(lotteryType_)) {
				pl3z6BallImg_ = new Bitmap[2];
				pl3z6BallImg_[PL3Z6_GRAY_BALL] = ballGray;
				pl3z6BallImg_[PL3Z6_RED_BALL] = ballYellow;

				pl3z6RedText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.FC3DZ6));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("PL5".equals(lotteryType_)) {
				pl5BallImg_ = new Bitmap[2];
				pl5BallImg_[PL5_GRAY_BALL] = ballGray;
				pl5BallImg_[PL5_RED_BALL] = ballYellow;

				pl5WanText = new TextView(bv_);
				pl5QianText = new TextView(bv_);
				pl5BaiText = new TextView(bv_);
				pl5ShiText = new TextView(bv_);
				pl5GeText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.PL5));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("QXC".equals(lotteryType_)) {
				qxcBallImg_ = new Bitmap[2];
				qxcBallImg_[QXC_GRAY_BALL] = ballGray;
				qxcBallImg_[QXC_RED_BALL] = ballYellow;

				qxcYiText = new TextView(bv_);
				qxcErText = new TextView(bv_);
				qxcSanText = new TextView(bv_);
				qxcSiText = new TextView(bv_);
				qxcWuText = new TextView(bv_);
				qxcLiuText = new TextView(bv_);
				qxcQiText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.PL5));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			} else if ("QLC".equals(lotteryType_)) {
				qlcBallImg_ = new Bitmap[2];
				qlcBallImg_[QXC_GRAY_BALL] = ballGray;
				qlcBallImg_[QXC_RED_BALL] = ballYellow;

				qlcRedText = new TextView(bv_);
			} else if ("SXL".equals(lotteryType_)) {
				sxlBallImg_ = new Bitmap[2];
				sxlBallImg_[SXL_GRAY_BALL] = ballGray;
				sxlBallImg_[SXL_RED_BALL] = ballYellow;

				sxlRedText = new TextView(bv_);

				// 提示文本
				TextView textView = new TextView(bv_);
				textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), LayoutParams.WRAP_CONTENT));
				textView.setPadding(leftMargin, 0, rightMargin, 0);
				textView.setText(getContext().getString(R.string.SXL));
				textView.setTextColor(0xFF000000);
				lotteryGroup.addView(textView);
			}
		}

		public Lottery newLottery() {
			return new Lottery(bv_);
		}

		class Lottery extends View {
			// 报文属性
			String ballType_;
			int ballNum_;
			int width_;

			// 行数、列数
			int row;
			int col;

			BallView ballView;

			public Lottery(Context context) {
				super(context);
				if (null == ballView) {
					ballView = new BallView(context);
				}

			}

			public void initLotteryResource() {
//				if ("DLT".equals(lotteryType_) || "SSQ".equals(lotteryType_) || "QLC".equals(lotteryType_) || "SXL".equals(lotteryType_)
//						|| "FC3DZ3".equals(lotteryType_) || "FC3DZ6".equals(lotteryType_) || "PL3Z3".equals(lotteryType_)
//						|| "PL3Z6".equals(lotteryType_)) {
//					col = lotteryGroup.width_ / (ballWidth + leftMargin);
//				} else if ("FC3DZX".equals(lotteryType_) || "PL5".equals(lotteryType_) || "QXC".equals(lotteryType_)
//						|| "PL3ZX".equals(lotteryType_)) {
//					col = lotteryGroup.width_ / (ballWidth + leftMargin) - 1;
//				}
                col=9;
                
				if (ballNum_ % preNum == 0) {
					row = ballNum_ / preNum;
				} else {
					row = ballNum_ / preNum + 1;
				}
			}
			
			void addBallView(ViewGroup lotteryLinearLayout){
				int line = (ballNum_ + (preNum - 1)) / preNum;
				for (int i = 0; i < line; i++) {
					RelativeLayout re = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.lottery_layout, null);
					LinearLayout line3=(LinearLayout) re.findViewById(R.id.linearLayout3);
					line3.setLayoutParams(new RelativeLayout.LayoutParams( ballWidth*9,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
					RelativeLayout.LayoutParams parm=(RelativeLayout.LayoutParams)line3.getLayoutParams();
					parm.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
					parm.width =  ballWidth*9;
					LinearLayout lines = (LinearLayout) re.findViewById(R.id.linearLayout1);
                   for (int j = 1; j <= ballNum_; j++) {
                	   String numStr = String.valueOf(j);
						if (numStr.length() == 1) {
							numStr = "0" + numStr;
						}
					int num=(j-1)/preNum;
					 if(num==i){
						lines.addView(new Ball(bv_, numStr, ballType_));
					}
				}
					lotteryLinearLayout.addView(re);
				}
			}
			
			void addBallViewOther(ViewGroup lotteryLinearLayout){
				int line = (ballNum_ + (preNum - 1)) / preNum;
				for (int i = 0; i < line; i++) {
					RelativeLayout re = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.lottery_layout, null);
					LinearLayout line3=(LinearLayout) re.findViewById(R.id.linearLayout3);
					line3.setLayoutParams(new RelativeLayout.LayoutParams( ballWidth*9,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
					RelativeLayout.LayoutParams parm=(RelativeLayout.LayoutParams)line3.getLayoutParams();
					parm.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
					parm.width = ballWidth*9;
					LinearLayout lines = (LinearLayout) re.findViewById(R.id.linearLayout1);
                   for (int j = 0; j < ballNum_; j++) {
                	   String numStr = String.valueOf(j);
//						if (numStr.length() == 1) {
//							numStr = "0" + numStr;
//						}
					int num=(j)/preNum;
					 if(num==i){
						lines.addView(new Ball(bv_, numStr, ballType_));
					}
				}
					lotteryLinearLayout.addView(re);
				}
			}

			public void addChildView() {
				if ("DLT".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12);
					textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), Utils.getScaledValueY(20)));
					textView.setPadding(leftMargin, 0, 0, 0);
					textView.setTextColor(0xFF000000);
					if ("1".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.atleastfrontarea));
					} else if ("2".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.atleastbackarea));
					}
					lotteryLinearLayout.addView(textView);
					ArrayList<Ball> list = new ArrayList<Ball>();
					addBallView(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("SSQ".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12.0f);
					textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), Utils.getScaledValueY(20)));
					textView.setPadding(leftMargin, 0, 0, 0);
					if ("1".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.atleastredball));
						textView.setTextColor(0xFFD22621);
					} else if ("2".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.atleastblueball));
						textView.setTextColor(0xFF007DC7);
					}
					lotteryLinearLayout.addView(textView);
					addBallView(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("FC3DZX".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12.0f);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TEXT_WIDTH, TEXT_HEIGHT);
					params.topMargin = topMargin;
					textView.setLayoutParams(params);

					textView.setBackgroundResource(R.drawable.indicator);
					textView.setPadding(leftMargin, 2, 0, 0);
					textView.setTextColor(0xFF000000);
					if ("1".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.handred));
					} else if ("2".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.ten));
					} else if ("3".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.one));
					}
					lotteryLinearLayout.addView(textView);

					LinearLayout lotteryLine = new LinearLayout(bv_);
					lotteryLine.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLine.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLine);
					lotteryLinearLayout.addView(lotteryLine);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("FC3DZ3".equals(lotteryType_)) {
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("FC3DZ6".equals(lotteryType_)) {
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("PL3ZX".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TEXT_WIDTH, TEXT_HEIGHT);
					params.topMargin = topMargin;
					textView.setLayoutParams(params);

					textView.setBackgroundResource(R.drawable.indicator);
					textView.setPadding(leftMargin, 2, 0, 0);
					textView.setTextColor(0xFF000000);
					if ("1".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.handred));
					} else if ("2".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.ten));
					} else if ("3".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.one));
					}
					lotteryLinearLayout.addView(textView);

					LinearLayout lotteryLine = new LinearLayout(bv_);
					lotteryLine.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLine.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLine);
					lotteryLinearLayout.addView(lotteryLine);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("PL3Z3".equals(lotteryType_)) {
//					int height = topMargin + (ballHeight + topMargin) * row;
//					for (int num = 0; num < ballNum_; num++) {
//						String numStr = String.valueOf(num);
//						ballView.addBall(new Ball(bv_, numStr, ballType_), num, row, col);
//					}
//					ballView.setLayoutParams(new LayoutParams(width_, height));
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("PL3Z6".equals(lotteryType_)) {
//					int height = topMargin + (ballHeight + topMargin) * row;
//					for (int num = 0; num < ballNum_; num++) {
//						String numStr = String.valueOf(num);
//						ballView.addBall(new Ball(bv_, numStr, ballType_), num, row, col);
//					}
//					ballView.setLayoutParams(new LayoutParams(width_, height));
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("PL5".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TEXT_WIDTH, TEXT_HEIGHT);
					params.topMargin = topMargin;
					textView.setLayoutParams(params);

					textView.setBackgroundResource(R.drawable.indicator);
					textView.setPadding(leftMargin, 2, 0, 0);
					textView.setTextColor(0xFF000000);
					if ("1".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.tenthousand));
					} else if ("2".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.thousand));
					} else if ("3".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.handred));
					} else if ("4".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.ten));
					} else if ("5".equals(ballType_)) {
						textView.setText(getContext().getString(R.string.one));
					}
					lotteryLinearLayout.addView(textView);

					LinearLayout lotteryLine = new LinearLayout(bv_);
					lotteryLine.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLine.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLine);
					lotteryLinearLayout.addView(lotteryLine);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("QXC".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout horLotteryLinearLayout = new LinearLayout(bv_);
					horLotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					horLotteryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TEXT_WIDTH, TEXT_HEIGHT);
					params.topMargin = topMargin;
					textView.setLayoutParams(params);

					textView.setBackgroundResource(R.drawable.indicator);
					textView.setPadding(leftMargin, 2, 0, 0);
					textView.setTextColor(0xFF000000);
					String[] numbers = getContext().getResources().getStringArray(R.array.weekdays);
					if ("1".equals(ballType_)) {
						textView.setText(numbers[0]);
					} else if ("2".equals(ballType_)) {
						textView.setText(numbers[1]);
					} else if ("3".equals(ballType_)) {
						textView.setText(numbers[2]);
					} else if ("4".equals(ballType_)) {
						textView.setText(numbers[3]);
					} else if ("5".equals(ballType_)) {
						textView.setText(numbers[4]);
					} else if ("6".equals(ballType_)) {
						textView.setText(numbers[5]);
					} else if ("7".equals(ballType_)) {
						textView.setText(numbers[6]);
					}
					horLotteryLinearLayout.addView(textView);

					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallViewOther(lotteryLinearLayout);
					horLotteryLinearLayout.addView(lotteryLinearLayout);
					lotteryGroup.addView(horLotteryLinearLayout);
				} else if ("QLC".equals(lotteryType_)) {
					int height = topMargin + (ballHeight + topMargin) * row;
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);

					TextView textView = new TextView(bv_);
					textView.setTextSize(12);
					textView.setLayoutParams(new LayoutParams(Utils.getScaledValueX(width_), Utils.getScaledValueY(20)));
					textView.setPadding(leftMargin, 0, 0, 0);
					textView.setText(getContext().getString(R.string.atleast7balls));
					textView.setTextColor(0xFF000000);
					lotteryLinearLayout.addView(textView);

                     addBallView(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				} else if ("SXL".equals(lotteryType_)) {
					
					LinearLayout lotteryLinearLayout = new LinearLayout(bv_);
					lotteryLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					lotteryLinearLayout.setOrientation(LinearLayout.VERTICAL);
					addBallView(lotteryLinearLayout);
					lotteryGroup.addView(lotteryLinearLayout);
				}
			}
		}

		class BallView extends RelativeLayout {
			public BallView(Context context) {
				super(context);
			}

			public void addBall(Ball ball, int ballNum, int row, int col) {
				RelativeLayout.LayoutParams layoutParams = (LayoutParams) ball.getLayoutParams();

				if ("FC3DZX".equals(lotteryType_) || "PL5".equals(lotteryType_) || "QXC".equals(lotteryType_)
						|| "FC3DZ3".equals(lotteryType_) || "FC3DZ6".equals(lotteryType_) || "PL3ZX".equals(lotteryType_)
						|| "PL3Z3".equals(lotteryType_) || "PL3Z6".equals(lotteryType_)) {
					ballNum += 1;
				}

				// 当前所属行
				int currentRow = ballNum / col + 1;

				// 临界球(col的倍数球)所属行
				for (int j = 1; j <= row; j++) {
					if (ballNum % col == 0 && ballNum / col == j) {
						currentRow = j;
						break;
					}
				}

				// 计算球的位置
				layoutParams.leftMargin = Utils.getScaledValueX((ballNum - col * (currentRow - 1)) * leftMargin + (ballNum - col * (currentRow - 1) - 1)
						* ballWidth);
				layoutParams.topMargin = Utils.getScaledValueY(topMargin + (topMargin + ballHeight) * (currentRow - 1));
				ball.setLayoutParams(layoutParams);

				this.addView(ball);
			}
		}

		class Ball extends TextView {
			String text_;
			String currentBallType_;
			int type = GRAY_BALL;

			public Ball(Context context, String text, String ballType) {
				super(context);
				text_ = text;
				currentBallType_ = ballType;
				setLayoutParams(new RelativeLayout.LayoutParams(ballWidth,ballHeight));
			}

			@Override
			protected void onDraw(Canvas canvas) {
				paint_.setFakeBoldText(true); // 粗体
				paint_.setTextSize(Utils.getFontSize(10));
				if ("DLT".equals(lotteryType_)) {
					canvas.drawBitmap(dltBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((dltBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((dltBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("SSQ".equals(lotteryType_)) {
					canvas.drawBitmap(ssqBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((ssqBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((ssqBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("FC3DZX".equals(lotteryType_)) {
					canvas.drawBitmap(fc3dzxBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((fc3dzxBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((fc3dzxBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("FC3DZ3".equals(lotteryType_)) {
					canvas.drawBitmap(fc3dz3BallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((fc3dz3BallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((fc3dz3BallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("FC3DZ6".equals(lotteryType_)) {
					canvas.drawBitmap(fc3dz6BallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((fc3dz6BallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((fc3dz6BallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("PL3ZX".equals(lotteryType_)) {
					canvas.drawBitmap(pl3zxBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((pl3zxBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((pl3zxBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("PL3Z3".equals(lotteryType_)) {
					canvas.drawBitmap(pl3z3BallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((pl3z3BallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((pl3z3BallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("PL3Z6".equals(lotteryType_)) {
					canvas.drawBitmap(pl3z6BallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((pl3z6BallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((pl3z6BallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("PL5".equals(lotteryType_)) {
					canvas.drawBitmap(pl5BallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((pl5BallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((pl5BallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("QXC".equals(lotteryType_)) {
					canvas.drawBitmap(qxcBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((qxcBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((qxcBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("QLC".equals(lotteryType_)) {
					canvas.drawBitmap(qlcBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((qlcBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((qlcBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				} else if ("SXL".equals(lotteryType_)) {
					canvas.drawBitmap(sxlBallImg_[type], 0, 0, paint_);
					canvas.drawText(text_, ((sxlBallImg_[type].getWidth() - paint_.measureText(text_)) / 2), ((sxlBallImg_[type]
							.getHeight() + paint_.getTextSize()) / 2) - 2, paint_);
				}
			}

			@Override
			public boolean onTouchEvent(MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					// 可以加上震动效果
					break;
				case MotionEvent.ACTION_UP:
					changeBallStatus(this, currentBallType_);
					break;
				default:
					return super.onTouchEvent(event);
				}
				return true;
			}
		}

		public void changeBallStatus(Ball ball, String currentBallType) {
			// 大乐透
			if ("DLT".equals(lotteryType_)) {
				if (ball.type == DLT_GRAY_BALL) {
					// 红球
					if ("1".equals(currentBallType)) {
						ball.type = DLT_RED_BALL;

						setFinalText(dltRedText, ball);
						dltRedNum_++;
					}
					// 蓝球
					else if ("2".equals(currentBallType)) {
						ball.type = DLT_BLUE_BALL;

						setFinalText(dltBlueText, ball);
						dltBlueNum_++;
					}
				} else if (ball.type == DLT_RED_BALL) {
					ball.type = DLT_GRAY_BALL;
					String finalRedText = removeNum(dltRedText.getText().toString(), ball.text_);
					dltRedText.setText(finalRedText);
					dltRedNum_--;
				} else if (ball.type == DLT_BLUE_BALL) {
					ball.type = DLT_GRAY_BALL;
					String finalBlueText = removeNum(dltBlueText.getText().toString(), ball.text_);
					dltBlueText.setText(finalBlueText);
					dltBlueNum_--;
				}

				String dltRedBallText = dltRedText.getText().toString();
				String dltBlueBallText = dltBlueText.getText().toString();

				String resultText = dltRedBallText + "|" + dltBlueBallText;
				String endResultText = resetNum(resultText, true);

				setNumText(numTextView, endResultText);
			}
			// 双色球
			else if ("SSQ".equals(lotteryType_)) {
				if (ball.type == SSQ_GRAY_BALL) {
					// 红球
					if ("1".equals(currentBallType)) {
						ball.type = SSQ_RED_BALL;

						setFinalText(ssqRedText, ball);
						ssqRedNum_++;
					}
					// 蓝球
					else if ("2".equals(currentBallType)) {
						ball.type = SSQ_BLUE_BALL;

						setFinalText(ssqBlueText, ball);
						ssqBlueNum_++;
					}
				} else if (ball.type == SSQ_RED_BALL) {
					ball.type = SSQ_GRAY_BALL;
					String finalRedText = removeNum(ssqRedText.getText().toString(), ball.text_);
					ssqRedText.setText(finalRedText);
					ssqRedNum_--;
				} else if (ball.type == SSQ_BLUE_BALL) {
					ball.type = SSQ_GRAY_BALL;
					String finalBlueText = removeNum(ssqBlueText.getText().toString(), ball.text_);
					ssqBlueText.setText(finalBlueText);
					ssqBlueNum_--;
				}

				String ssqRedBallText = ssqRedText.getText().toString();
				String ssqBlueBallText = ssqBlueText.getText().toString();

				String resultText = ssqRedBallText + "|" + ssqBlueBallText;
				String endResultText = resetNum(resultText, true);

				setNumText(numTextView, endResultText);
			}
			// 福彩3D-直选
			else if ("FC3DZX".equals(lotteryType_)) {
				if (ball.type == FC3DZX_GRAY_BALL) {
					ball.type = FC3DZX_RED_BALL;
					if ("1".equals(currentBallType)) {
						setFinalText(fc3dzxBaiText, ball);
						fc3dzxBaiNum_++;
					} else if ("2".equals(currentBallType)) {
						setFinalText(fc3dzxShiText, ball);
						fc3dzxShiNum_++;
					} else if ("3".equals(currentBallType)) {
						setFinalText(fc3dzxGeText, ball);
						fc3dzxGeNum_++;
					}
				} else if (ball.type == FC3DZX_RED_BALL) {
					ball.type = FC3DZX_GRAY_BALL;
					if ("1".equals(currentBallType)) {
						String fc3dzxFinalBaiText = removeNum(fc3dzxBaiText.getText().toString(), ball.text_);
						fc3dzxBaiText.setText(fc3dzxFinalBaiText);
						fc3dzxBaiNum_--;
					} else if ("2".equals(currentBallType)) {
						String fc3dzxFinalShiText = removeNum(fc3dzxShiText.getText().toString(), ball.text_);
						fc3dzxShiText.setText(fc3dzxFinalShiText);
						fc3dzxShiNum_--;
					} else if ("3".equals(currentBallType)) {
						String fc3dzxFinalGeText = removeNum(fc3dzxGeText.getText().toString(), ball.text_);
						fc3dzxGeText.setText(fc3dzxFinalGeText);
						fc3dzxGeNum_--;
					}
				}

				String fc3dzxBaiBallText = fc3dzxBaiText.getText().toString();
				String fc3dzxShiBallText = fc3dzxShiText.getText().toString();
				String fc3dzxGeBallText = fc3dzxGeText.getText().toString();

				String resultText = fc3dzxBaiBallText + "|" + fc3dzxShiBallText + "|" + fc3dzxGeBallText;
				String endResultText = resetNum(resultText, false);

				setNumText(numTextView, endResultText);
			}
			// 福彩3D-组三
			else if ("FC3DZ3".equals(lotteryType_)) {
				if (ball.type == FC3DZ3_GRAY_BALL) {
					ball.type = FC3DZ3_RED_BALL;

					setFinalText(fc3dz3RedText, ball);
					fc3dz3RedNum_++;
				} else if (ball.type == FC3DZ3_RED_BALL) {
					ball.type = FC3DZ3_GRAY_BALL;

					String finalRedText = removeNum(fc3dz3RedText.getText().toString(), ball.text_);
					fc3dz3RedText.setText(finalRedText);
					fc3dz3RedNum_--;
				}

				String fc3dz3RedBallText = fc3dz3RedText.getText().toString();

				String endResultText = resetNum(fc3dz3RedBallText, false);

				setNumText(numTextView, endResultText);
			}
			// 福彩3D-组六
			else if ("FC3DZ6".equals(lotteryType_)) {
				if (ball.type == FC3DZ6_GRAY_BALL) {
					ball.type = FC3DZ6_RED_BALL;

					setFinalText(fc3dz6RedText, ball);
					fc3dz6RedNum_++;
				} else if (ball.type == FC3DZ6_RED_BALL) {
					ball.type = FC3DZ6_GRAY_BALL;

					String finalRedText = removeNum(fc3dz6RedText.getText().toString(), ball.text_);
					fc3dz6RedText.setText(finalRedText);
					fc3dz6RedNum_--;
				}

				String fc3dz6RedBallText = fc3dz6RedText.getText().toString();

				String endResultText = resetNum(fc3dz6RedBallText, false);

				setNumText(numTextView, endResultText);
			}
			// 排列三-直选
			else if ("PL3ZX".equals(lotteryType_)) {
				if (ball.type == PL3ZX_GRAY_BALL) {
					ball.type = PL3ZX_RED_BALL;
					if ("1".equals(currentBallType)) {
						setFinalText(pl3zxBaiText, ball);
						pl3zxBaiNum_++;
					} else if ("2".equals(currentBallType)) {
						setFinalText(pl3zxShiText, ball);
						pl3zxShiNum_++;
					} else if ("3".equals(currentBallType)) {
						setFinalText(pl3zxGeText, ball);
						pl3zxGeNum_++;
					} 
				} else if (ball.type == PL3ZX_RED_BALL) {
					ball.type = PL3ZX_GRAY_BALL;
					if ("1".equals(currentBallType)) {
						String pl3zxFinalBaiText = removeNum(pl3zxBaiText.getText().toString(), ball.text_);
						pl3zxBaiText.setText(pl3zxFinalBaiText);
						pl3zxBaiNum_--;
					} else if ("2".equals(currentBallType)) {
						String pl3zxFinalShiText = removeNum(pl3zxShiText.getText().toString(), ball.text_);
						pl3zxShiText.setText(pl3zxFinalShiText);
						pl3zxShiNum_--;
					} else if ("3".equals(currentBallType)) {
						String pl3zxFinalGeText = removeNum(pl3zxGeText.getText().toString(), ball.text_);
						pl3zxGeText.setText(pl3zxFinalGeText);
						pl3zxGeNum_--;
					}
				}

				String pl3zxBaiBallText = pl3zxBaiText.getText().toString();
				String pl3zxShiBallText = pl3zxShiText.getText().toString();
				String pl3zxGeBallText = pl3zxGeText.getText().toString();

				String resultText = pl3zxBaiBallText + "|" + pl3zxShiBallText + "|" + pl3zxGeBallText;
				String endResultText = resetNum(resultText, false);

				setNumText(numTextView, endResultText);
			}
			// 排列三-组三
			else if ("PL3Z3".equals(lotteryType_)) {
				if (ball.type == PL3Z3_GRAY_BALL) {
					ball.type = PL3Z3_RED_BALL;

					setFinalText(pl3z3RedText, ball);
					pl3z3RedNum_++;
				} else if (ball.type == PL3Z3_RED_BALL) {
					ball.type = PL3Z3_GRAY_BALL;

					String finalRedText = removeNum(pl3z3RedText.getText().toString(), ball.text_);
					pl3z3RedText.setText(finalRedText);
					pl3z3RedNum_--;
				}

				String pl3z3RedBallText = pl3z3RedText.getText().toString();

				String endResultText = resetNum(pl3z3RedBallText, false);

				setNumText(numTextView, endResultText);
			}
			// 排列三-组六
			else if ("PL3Z6".equals(lotteryType_)) {
				if (ball.type == PL3Z6_GRAY_BALL) {
					ball.type = PL3Z6_RED_BALL;

					setFinalText(pl3z6RedText, ball);
					pl3z6RedNum_++;
				} else if (ball.type == PL3Z6_RED_BALL) {
					ball.type = PL3Z6_GRAY_BALL;

					String finalRedText = removeNum(pl3z6RedText.getText().toString(), ball.text_);
					pl3z6RedText.setText(finalRedText);
					pl3z6RedNum_--;
				}

				String pl3z6RedBallText = pl3z6RedText.getText().toString();

				String endResultText = resetNum(pl3z6RedBallText, false);

				setNumText(numTextView, endResultText);
			}
			// 排列五
			else if ("PL5".equals(lotteryType_)) {
				if (ball.type == PL5_GRAY_BALL) {
					ball.type = PL5_RED_BALL;
					if ("1".equals(currentBallType)) {
						setFinalText(pl5WanText, ball);
						pl5WanNum_++;
					} else if ("2".equals(currentBallType)) {
						setFinalText(pl5QianText, ball);
						pl5QianNum_++;
					} else if ("3".equals(currentBallType)) {
						setFinalText(pl5BaiText, ball);
						pl5BaiNum_++;
					} else if ("4".equals(currentBallType)) {
						setFinalText(pl5ShiText, ball);
						pl5ShiNum_++;
					} else if ("5".equals(currentBallType)) {
						setFinalText(pl5GeText, ball);
						pl5GeNum_++;
					}
				} else if (ball.type == PL5_RED_BALL) {
					ball.type = PL5_GRAY_BALL;
					if ("1".equals(currentBallType)) {
						String pl5FinalWanText = removeNum(pl5WanText.getText().toString(), ball.text_);
						pl5WanText.setText(pl5FinalWanText);
						pl5WanNum_--;
					} else if ("2".equals(currentBallType)) {
						String pl5FinalQianText = removeNum(pl5QianText.getText().toString(), ball.text_);
						pl5QianText.setText(pl5FinalQianText);
						pl5QianNum_--;
					} else if ("3".equals(currentBallType)) {
						String pl5FinalBaiText = removeNum(pl5BaiText.getText().toString(), ball.text_);
						pl5BaiText.setText(pl5FinalBaiText);
						pl5BaiNum_--;
					} else if ("4".equals(currentBallType)) {
						String pl5FinalShiText = removeNum(pl5ShiText.getText().toString(), ball.text_);
						pl5ShiText.setText(pl5FinalShiText);
						pl5ShiNum_--;
					} else if ("5".equals(currentBallType)) {
						String pl5FinalGeText = removeNum(pl5GeText.getText().toString(), ball.text_);
						pl5GeText.setText(pl5FinalGeText);
						pl5GeNum_--;
					}
				}

				String pl5WanBallText = pl5WanText.getText().toString();
				String pl5QianBallText = pl5QianText.getText().toString();
				String pl5BaiBallText = pl5BaiText.getText().toString();
				String pl5ShiBallText = pl5ShiText.getText().toString();
				String pl5GeBallText = pl5GeText.getText().toString();

				String resultText = pl5WanBallText + "|" + pl5QianBallText + "|" + pl5BaiBallText + "|" + pl5ShiBallText + "|"
						+ pl5GeBallText;
				String endResultText = resetNum(resultText, false);

				setNumText(numTextView, endResultText);
			}
			// 七星彩
			else if ("QXC".equals(lotteryType_)) {
				if (ball.type == QXC_GRAY_BALL) {
					ball.type = QXC_RED_BALL;
					if ("1".equals(currentBallType)) {
						setFinalText(qxcYiText, ball);
						qxcYiNum_++;
					} else if ("2".equals(currentBallType)) {
						setFinalText(qxcErText, ball);
						qxcErNum_++;
					} else if ("3".equals(currentBallType)) {
						setFinalText(qxcSanText, ball);
						qxcSanNum_++;
					} else if ("4".equals(currentBallType)) {
						setFinalText(qxcSiText, ball);
						qxcSiNum_++;
					} else if ("5".equals(currentBallType)) {
						setFinalText(qxcWuText, ball);
						qxcWuNum_++;
					} else if ("6".equals(currentBallType)) {
						setFinalText(qxcLiuText, ball);
						qxcLiuNum_++;
					} else if ("7".equals(currentBallType)) {
						setFinalText(qxcQiText, ball);
						qxcQiNum_++;
					}
				} else if (ball.type == QXC_RED_BALL) {
					ball.type = QXC_GRAY_BALL;
					if ("1".equals(currentBallType)) {
						String qxcFinalYiText = removeNum(qxcYiText.getText().toString(), ball.text_);
						qxcYiText.setText(qxcFinalYiText);
						qxcYiNum_--;
					} else if ("2".equals(currentBallType)) {
						String qxcFinalErText = removeNum(qxcErText.getText().toString(), ball.text_);
						qxcErText.setText(qxcFinalErText);
						qxcErNum_--;
					} else if ("3".equals(currentBallType)) {
						String qxcFinalSanText = removeNum(qxcSanText.getText().toString(), ball.text_);
						qxcSanText.setText(qxcFinalSanText);
						qxcSanNum_--;
					} else if ("4".equals(currentBallType)) {
						String qxcFinalSiText = removeNum(qxcSiText.getText().toString(), ball.text_);
						qxcSiText.setText(qxcFinalSiText);
						qxcSiNum_--;
					} else if ("5".equals(currentBallType)) {
						String qxcFinalWuText = removeNum(qxcWuText.getText().toString(), ball.text_);
						qxcWuText.setText(qxcFinalWuText);
						qxcWuNum_--;
					} else if ("6".equals(currentBallType)) {
						String qxcFinalLiuText = removeNum(qxcLiuText.getText().toString(), ball.text_);
						qxcLiuText.setText(qxcFinalLiuText);
						qxcLiuNum_--;
					} else if ("7".equals(currentBallType)) {
						String qxcFinalQiText = removeNum(qxcQiText.getText().toString(), ball.text_);
						qxcQiText.setText(qxcFinalQiText);
						qxcQiNum_--;
					}
				}

				String qxcYiBallText = qxcYiText.getText().toString();
				String qxcErBallText = qxcErText.getText().toString();
				String qxcSanBallText = qxcSanText.getText().toString();
				String qxcSiBallText = qxcSiText.getText().toString();
				String qxcWuBallText = qxcWuText.getText().toString();
				String qxcLiuBallText = qxcLiuText.getText().toString();
				String qxcQiBallText = qxcQiText.getText().toString();

				String resultText = qxcYiBallText + "|" + qxcErBallText + "|" + qxcSanBallText + "|" + qxcSiBallText + "|" + qxcWuBallText
						+ "|" + qxcLiuBallText + "|" + qxcQiBallText;
				String endResultText = resetNum(resultText, false);

				setNumText(numTextView, endResultText);
			}
			// 七乐彩
			else if ("QLC".equals(lotteryType_)) {
				if (ball.type == QLC_GRAY_BALL) {
					ball.type = QLC_RED_BALL;

					setFinalText(qlcRedText, ball);
					qlcRedNum_++;
				} else if (ball.type == QLC_RED_BALL) {
					ball.type = QLC_GRAY_BALL;

					String finalRedText = removeNum(qlcRedText.getText().toString(), ball.text_);
					qlcRedText.setText(finalRedText);
					qlcRedNum_--;
				}

				String qlcRedBallText = qlcRedText.getText().toString();

				String endResultText = resetNum(qlcRedBallText, true);

				setNumText(numTextView, endResultText);
			}
			// 生肖乐
			else if ("SXL".equals(lotteryType_)) {
				if (ball.type == SXL_GRAY_BALL) {
					ball.type = SXL_RED_BALL;

					setFinalText(sxlRedText, ball);
					sxlRedNum_++;
				} else if (ball.type == SXL_RED_BALL) {
					ball.type = SXL_GRAY_BALL;

					String finalRedText = removeNum(sxlRedText.getText().toString(), ball.text_);
					sxlRedText.setText(finalRedText);
					sxlRedNum_--;
				}

				String sxlRedBallText = sxlRedText.getText().toString();

				String endResultText = resetNum(sxlRedBallText, true);

				setNumText(numTextView, endResultText);
			}
			// 重新绘制
			lotteryGroup.invalidate();
		}

		private void setFinalText(TextView textView, Ball ball) {
			if (textView.getText().length() > 0) {
				textView.setText(textView.getText() + " " + ball.text_);
			} else {
				textView.setText(ball.text_);
			}
		}

		private void setNumText(TextView numTextView, String endResultText) {
			if (endResultText.length() > 0) {
				numTextView.setTextColor(0xFF000000);
				numTextView.setText(endResultText);
			} else {
				numTextView.setText("");
				numTextView.setHintTextColor(0xFF000000);
				numTextView.setHint(getContext().getString(R.string.notselectnumber));
			}
		}

		/**
		 * 重置
		 * 
		 * @param beginResStr
		 * @param isFillZero
		 * @return
		 */
		private String resetNum(String beginResStr, boolean isFillZero) {
			String endResStr = "";
			String[] beginResStrs = beginResStr.split("\\|");
			for (int i = 0; i < beginResStrs.length; i++) {
				if (beginResStrs[i].length() > 0) {
					endResStr += sortNum(beginResStrs[i], isFillZero) + "|";
				}
			}
			if (endResStr.length() > 0) {
				endResStr = endResStr.substring(0, endResStr.lastIndexOf("|"));
			}
			return endResStr;
		}

		/**
		 * 排序
		 * 
		 * @param currStr
		 * @return
		 */
		private String sortNum(String currStr, boolean isFillZero) {
			String finalStr = "";
			String[] strArr = currStr.split(" ");

			int[] values = new int[strArr.length];
			for (int s = 0; s < strArr.length; s++) {
				values[s] = Integer.valueOf(strArr[s]);
			}

			int temp;
			for (int i = 0; i < values.length; i++) {
				for (int j = 0; j < values.length - i - 1; j++) {
					if (values[j] > values[j + 1]) {
						temp = values[j];
						values[j] = values[j + 1];
						values[j + 1] = temp;
					}
				}
			}
			for (int v = 0; v < values.length; v++) {
				String curNum = String.valueOf(values[v]);
				if (isFillZero && curNum.length() == 1) {
					curNum = "0" + curNum;
				}
				if (finalStr.length() > 0) {
					finalStr = finalStr + " " + curNum;
				} else {
					finalStr = curNum;
				}
			}

			return finalStr;
		}

		/**
		 * 移除
		 * 
		 * @param currStr
		 * @param currNum
		 * @return
		 */
		private String removeNum(String currStr, String currNum) {
			String finalStr = "";
			String[] currStrArr = currStr.split(" ");
			for (String str : currStrArr) {
				if (!currNum.equals(str)) {
					if (finalStr.length() > 0) {
						finalStr = finalStr + " " + str;
					} else {
						finalStr = str;
					}
				}
			}
			return finalStr;
		}
		
		// 释放资源的方法。
		public void releaseResource(){
			if(dltBallImg_ != null){
				for (int i = 0; i < dltBallImg_.length; i++) {
					if(dltBallImg_[i] != null && !dltBallImg_[i].isRecycled()){
						dltBallImg_[i].recycle();
						dltBallImg_[i] = null;
					}
				}
			}
			if(ssqBallImg_ != null){
				for (int i = 0; i < ssqBallImg_.length; i++) {
					if(ssqBallImg_[i] != null && !ssqBallImg_[i].isRecycled()){
						ssqBallImg_[i].recycle();
						ssqBallImg_[i] = null;
					}
				}
			}
			if(fc3dzxBallImg_ != null){
				for (int i = 0; i < fc3dzxBallImg_.length; i++) {
					if(fc3dzxBallImg_[i] != null && !fc3dzxBallImg_[i].isRecycled()){
						fc3dzxBallImg_[i].recycle();
						fc3dzxBallImg_[i] = null;
					}
				}
			}
			if(fc3dz3BallImg_ != null){
				for (int i = 0; i < fc3dz3BallImg_.length; i++) {
					if(fc3dz3BallImg_[i] != null && !fc3dz3BallImg_[i].isRecycled()){
						fc3dz3BallImg_[i].recycle();
						fc3dz3BallImg_[i] = null;
					}
				}
			}
			if(fc3dz6BallImg_ != null){
				for (int i = 0; i < fc3dz6BallImg_.length; i++) {
					if(fc3dz6BallImg_[i] != null && !fc3dz6BallImg_[i].isRecycled()){
						fc3dz6BallImg_[i].recycle();
						fc3dz6BallImg_[i] = null;
					}
				}
			}
			if(pl3zxBallImg_ != null){
				for (int i = 0; i < pl3zxBallImg_.length; i++) {
					if(pl3zxBallImg_[i] != null && !pl3zxBallImg_[i].isRecycled()){
						pl3zxBallImg_[i].recycle();
						pl3zxBallImg_[i] = null;
					}
				}
			}
			if(pl3z3BallImg_ != null){
				for (int i = 0; i < pl3z3BallImg_.length; i++) {
					if(pl3z3BallImg_[i] != null && !pl3z3BallImg_[i].isRecycled()){
						pl3z3BallImg_[i].recycle();
						pl3z3BallImg_[i] = null;
					}
				}
			}
			if(pl3z6BallImg_ != null){
				for (int i = 0; i < pl3z6BallImg_.length; i++) {
					if(pl3z6BallImg_[i] != null && !pl3z6BallImg_[i].isRecycled()){
						pl3z6BallImg_[i].recycle();
						pl3z6BallImg_[i] = null;
					}
				}
			}
			if(pl5BallImg_ != null){
				for (int i = 0; i < pl5BallImg_.length; i++) {
					if(pl5BallImg_[i] != null && !pl5BallImg_[i].isRecycled()){
						pl5BallImg_[i].recycle();
						pl5BallImg_[i] = null;
					}
				}
			}
			if(qxcBallImg_ != null){
				for (int i = 0; i < qxcBallImg_.length; i++) {
					if(qxcBallImg_[i] != null && !qxcBallImg_[i].isRecycled()){
						qxcBallImg_[i].recycle();
						qxcBallImg_[i] = null;
					}
				}
			}
			if(qlcBallImg_ != null){
				for (int i = 0; i < qlcBallImg_.length; i++) {
					if(qlcBallImg_[i] != null && !qlcBallImg_[i].isRecycled()){
						qlcBallImg_[i].recycle();
						qlcBallImg_[i] = null;
					}
				}
			}
			if(sxlBallImg_ != null){
				for (int i = 0; i < sxlBallImg_.length; i++) {
					if(sxlBallImg_[i] != null && !sxlBallImg_[i].isRecycled()){
						sxlBallImg_[i].recycle();
						sxlBallImg_[i] = null;
					}
				}
			}
		}
	}

	/**
	 * 彩票控件提交内容
	 * 
	 * @return
	 */
	public String getSubmitValue() {
		LotteryGroup lotteryGroup = (LotteryGroup) realView_;
		String result = lotteryGroup.numTextView.getText().toString();
		String lotteryType = lotteryGroup.lotteryType_;

		StringBuffer sb = new StringBuffer();
		if (result.length() > 0) {
			// 双色球
			if ("SSQ".equals(lotteryType)) {
				String[] ssqs = result.split("\\|");
				for (int s = 0; s < ssqs.length; s++) {
					String prefix = "";
					if (s == 0) {
						prefix = "Red_";
					} else if (s == 1) {
						prefix = "Blue_";
					}
					if (ssqs[s].length() > 0) {
						String[] strs = ssqs[s].split(" ");
						for (int j = 0; j < strs.length; j++) {
							if ("0".equals(strs[j].substring(0, 1))) {
								sb.append(prefix).append(strs[j].substring(1, strs[j].length())).append("=").append(
										strs[j].substring(1, strs[j].length())).append("&");
							} else {
								sb.append(prefix).append(strs[j]).append("=").append(strs[j]).append("&");
							}
						}
					}
				}
			} else if ("FC3DZX".equals(lotteryType)) {
				String[] fc3dzxs = result.split("\\|");
				for (int f = 0; f < fc3dzxs.length; f++) {
					String prefix = "";
					if (f == 0) {
						prefix = "Zx_0_";
					} else if (f == 1) {
						prefix = "Zx_1_";
					} else if (f == 2) {
						prefix = "Zx_2_";
					}
					if (fc3dzxs[f].length() > 0) {
						String[] strs = fc3dzxs[f].split(" ");
						for (int j = 0; j < strs.length; j++) {
							sb.append(prefix).append(strs[j]).append("=").append(strs[j]).append("&");
						}
					}
				}
			} else if ("FC3DZ3".equals(lotteryType) || "FC3DZ6".equals(lotteryType)) {
				String prefix = "Zu_";
				String[] strs = result.split(" ");
				for (int j = 0; j < strs.length; j++) {
					sb.append(prefix).append(strs[j]).append("=").append(strs[j]).append("&");
				}
			} else if ("QLC".equals(lotteryType)) {
				String prefix = "Seven_";
				String[] strs = result.split(" ");
				for (int j = 0; j < strs.length; j++) {
					if ("0".equals(strs[j].substring(0, 1))) {
						sb.append(prefix).append(strs[j].substring(1, strs[j].length())).append("=").append(
								strs[j].substring(1, strs[j].length())).append("&");
					} else {
						sb.append(prefix).append(strs[j]).append("=").append(strs[j]).append("&");
					}
				}
			} else if ("QXC".equals(lotteryType)) {
				String[] qxcs = result.split("\\|");
				for (int q = 0; q < qxcs.length; q++) {
					String prefix = "";
					if (q == 0) {
						prefix = "No_1=";
					} else if (q == 1) {
						prefix = "No_2=";
					} else if (q == 2) {
						prefix = "No_3=";
					} else if (q == 3) {
						prefix = "No_4=";
					} else if (q == 4) {
						prefix = "No_5=";
					} else if (q == 5) {
						prefix = "No_6=";
					} else if (q == 6) {
						prefix = "No_7=";
					}
					if (qxcs[q].length() > 0) {
						String[] strs = qxcs[q].split(" ");
						sb.append(prefix);
						for (int j = 0; j < strs.length; j++) {
							sb.append(strs[j]);
						}
						sb.append("&");
					}
				}
			} else if ("PL5".equals(lotteryType) || "PL3ZX".equals(lotteryType)) {
				String[] qxcs = result.split("\\|");
				for (int q = 0; q < qxcs.length; q++) {
					String prefix = "";
					if (q == 0) {
						prefix = "No_0=";
					} else if (q == 1) {
						prefix = "No_1=";
					} else if (q == 2) {
						prefix = "No_2=";
					} else if (q == 3) {
						prefix = "No_3=";
					} else if (q == 4) {
						prefix = "No_4=";
					}
					if (qxcs[q].length() > 0) {
						String[] strs = qxcs[q].split(" ");
						sb.append(prefix);
						for (int j = 0; j < strs.length; j++) {
							sb.append(strs[j]);
						}
						sb.append("&");
					}
				}
			} else if ("PL3Z3".equals(lotteryType) || "PL3Z6".equals(lotteryType)) {
				String prefix = "zuNo=";
				String[] strs = result.split(" ");
				sb.append(prefix);
				for (int j = 0; j < strs.length; j++) {
					sb.append(strs[j]);
				}
				sb.append("&");
			} else if ("DLT".equals(lotteryType)) {
				String[] dlts = result.split("\\|");
				for (int d = 0; d < dlts.length; d++) {
					String prefix = "";
					if (d == 0) {
						prefix = "frontNo=";
					} else if (d == 1) {
						prefix = "backNo=";
					}
					if (dlts[d].length() > 0) {
						String[] strs = dlts[d].split(" ");
						sb.append(prefix);
						for (int j = 0; j < strs.length; j++) {
							sb.append(strs[j]);
						}
						sb.append("&");
					}
				}
			} else if ("SXL".equals(lotteryType)) {
				String prefix = "animalNo=";
				String[] strs = result.split(" ");
				sb.append(prefix);
				for (int j = 0; j < strs.length; j++) {
					sb.append(strs[j]);
				}
				sb.append("&");
			}
		}

		String sReturn = "";
		int len = sb.length();
		if (len > 0) {
			// 删除最后一个 & 符号.
			if (sb.lastIndexOf("&") == len - 1) {
				sReturn = sb.delete(len - 1, len).toString();
			} else {
				sReturn = sb.toString();
			}
		}
		return sReturn;
	}
	
	@Override
	public void releaseResource(Component component) {
		super.releaseResource(component);
		if(null != realView_){
			((LotteryGroup) realView_).releaseResource();			
		}
	}
}
